<template>
  <view>
    <my-search @click="gotoSearch"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧滑动区域 -->
      <scroll-view scroll-y="true" class="scroll-left-box" :style="{height:wd+'px'}">
        <block v-for="(item,i) in cateList" :key="i">
          <view :class="['scroll-view-left',i === active ? 'active': '' ]" @click="activeHeadle(i)">{{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧滑动区域 -->
      <scroll-view scroll-y="true" class="scroll-right-box" :style="{height:wd+'px'}" :scroll-top="scrollTop">
        <!-- 二级标题 -->
        <view class="cate-level" v-for="(item2,i2) in cateLevel" :key="i2">
          <view class="cate-level-title">
            {{item2.cat_name}}
          </view>
          <!-- 三级分类 -->
          <view class="cate-level-content">
            <view class="level-content-item" v-for="(item3,i3) in item2.children" :key="i3" @click="goToGoosList(item3)">
              <!-- 图片 -->
              <image :src="item3.cat_icon"></image>
              <!-- 文字 -->
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from "../../mixins/tabbar-badge.js"
  export default {
    mixins:[badgeMix],
    data() {
      return {
        wd: 0,
        cateList: [],
        active: 0,
        cateLevel: [],
        scrollTop:0,
      }
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) {
          return uni.showMsg()
        }
        this.cateList = res.message
        this.cateLevel = res.message[0].children
        console.log(res.message[0].children)
      },
      activeHeadle(i) {
        this.active = i;
        // 点击获取2级数据
        this.cateLevel=this.cateList[i].children
        // 让滚动条回到顶部
        this.scrollTop=this.scrollTop===0?"1":"0"
      },
      // 三级跳转到goods——list
      goToGoosList(item3){
        // console.log(item3)
        uni.navigateTo({
          url:"../../subpak/goods_list/goods_list?cid"+item3.cat_id
        })
      },
      gotoSearch(){
        // console.log("ok")
        uni.navigateTo({
          url:"../../subpak/search/search"
        })
      }
    },
    onLoad() {
      const sysIfo = uni.getSystemInfoSync()
      // console.log(sysIfo.windowHeight)
      this.wd = sysIfo.windowHeight-40;
      this.getCateList()
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .scroll-left-box {
      width: 240rpx;

      .scroll-view-left {
        line-height: 80rpx;
        text-align: center;
        background-color: #F7F7F7;
        font-size: 24rpx;

        &.active {
          background-color: #fff;
          position: relative;
          color: #92c5f1;

          &::before {
            content: "";
            display: block;
            position: absolute;
            width: 3px;
            height: 60rpx;
            border-radius: 2rpx;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            background-color: #1296DB;
          }
        }
      }
    }

    .scroll-right-box {
      .cate-level-title {
        text-align: center;
        line-height: 60rpx;
        font-size: 24rpx;
        font-weight: bold;
      }

      .cate-level-content {
        display: flex;
        flex-wrap: wrap;

        .level-content-item {
          display: flex;
          flex-direction: column;
          width: 33.33%;
          align-items: center;
          margin-bottom: 10px;

          image {
            width: 120rpx;
            height: 120rpx;
          }
          text{
            font-size: 12px;
            padding: 10rpx 0;
          }
        }
      }

    }

  }
</style>
